let v = new Vue({
    el:'#main-container',
    data:function () {
        return {
            settings:{
                data:{
                    simpleData: {
                        enable:true,
                        pIdKey:'parentId'
                    }
                },
                callback:{
                    onClick:this.onClick,
                },
                view:{
                },
                check:{
                    enable:true,
                }
            },
            nodes:[],
            officeNodes:[],
            role:{},
            officeTreeObj:'',
            treeObj:{},
            offName:'',
            oldOffice:[],
            oldResources:[],
        }
    },
    methods:{
        initTree:function () {
            axios({
                url:'manager/resource/list',
                method: 'get',
                params:{rid:this.role.id},
            }).then(resp => {
                this.nodes = resp.data.obj.allResource;
                this.oldResources = resp.data.obj.resource;
                this.nodes[this.nodes.length] = {id:0,name:'全部权限',open:true};
                for (let i in this.nodes) {
                    for (let j in this.oldResources) {
                        if(this.nodes[i].id == this.oldResources[j].id){
                            this.nodes[i].checked = true;
                            break;
                        }
                    }
                }
                this.treeObj = $.fn.zTree.init($("#select-treetreeSelectResEdit"),this.settings,this.nodes);

            }).catch(error => {
                console.log(error);
            });
        },
        initOfficeTree:function () {
            axios({
                url:'manager/office/selectByRid',
                method: 'get',
                params:{rid:this.role.id}
            }).then(resp => {
                this.officeNodes = resp.data.obj.all;
                this.officeNodes[this.officeNodes.length] = {id:0,name:'全部部门',open:true};
                $("#treeSelectOfficeEdit").css("display","inline");
                this.oldOffice = resp.data.obj.office;
                for (let i in this.officeNodes) {
                    for (let j in this.oldOffice) {
                        if(this.oldOffice[j].id == this.officeNodes[i].id){
                            this.officeNodes[i].checked = true;
                            break;
                        }
                    }
                }
                this.officeTreeObj = $.fn.zTree.init($("#select-treetreeSelectOfficeEdit"),this.settings,this.officeNodes);

            }).catch(error => {
                console.log(error);
                layer.msg(error.message);
            });
        },
        selectRole:function () {
            axios({
                url:'manager/role/selectRole',
                params:{rid:this.role.id}
            }).then(resp => {
                this.role = resp.data.obj;
            }).catch(error => {
                layer.msg(error.message);
            })
        },
        changeOfficeShow:function () {
            //原本有变没有
            if(this.role.dataScope != '9'){
                $('#treeSelectOfficeEdit').css("display","none")
            }else {
                //原本没有变有
                //a.原本没有树
                if(this.officeTreeObj === undefined || this.officeTreeObj === ''){
                    this.initOfficeTree();
                }else{
                    //b.原本有树
                    $('#treeSelectOfficeEdit').css("display","inline");
                }

            }
        },
        toSelect:function () {
            layer.open({
                type: 2,
                title: '归属机构',
                area: ['80%', '80%'],
                content: 'manager/role/toSelect',
                end:() => {
                    // if(!layer.success){
                    //     this.selectByPage(1,this.pageInfo.pageSize);
                    // }
                    this.role.officeId = layer.offId;
                    this.offName = layer.offName;
                }
            })
        },
        doUpdate:function () {
            let updateResources = this.treeObj.getCheckedNodes(true);
            if(updateResources !== undefined && updateResources.length !== 0){
                if(updateResources[0].id === 0){
                    updateResources.splice(0,1);
                }
            }
            let flag = false;
            for (let i in updateResources) {
                for (let j in this.oldResources) {
                    if(updateResources[i].id !== this.oldResources[j].id
                        || (updateResources.length === 0 && this.oldResources.length !== 0)){
                        //要更新
                        flag = true;
                        break;
                    }
                }
            }
            if(flag){
                this.role.resources = updateResources;
            }else {
                this.role.resources = undefined;
            }
            flag = false;
            // this.role.oldResources = this.oldResources;
            if(this.officeTreeObj !== undefined && this.officeTreeObj !== ''){
                let updateOffeces = this.officeTreeObj.getCheckedNodes(true);
                if(updateOffeces.length !== 0 && updateOffeces[0].id === 0  ){
                    updateOffeces.splice(0,1);
                }
                for (let i in updateOffeces) {
                    for (let j in this.oldOffice) {
                        if(updateOffeces[i].id !== this.oldOffice[j].id
                            || (updateOffeces.length === 0 && this.oldOffice.length !== 0)){
                            //要更新
                            flag = true;
                            break;
                        }
                    }
                }
                if(flag){
                    this.role.offices = updateOffeces;
                }else{
                    this.role.offices = undefined;
                }
                // this.role.oldOffices = this.oldOffice;
            }else{
                this.role.offices = undefined;
                this.role.oldOffices = undefined;
            }
            axios({
                url:'manager/role/doUpdate',
                method:'put',
                data:this.role,
            }).then(resp => {
                let index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                parent.layer.msg(resp.data.msg);
            }).catch(error => {
                console.log(error);
                layer.msg(error.message);
            })
        }
    },
    created:function () {
        this.role = parent.layer.obj;
        this.selectRole();
    },
    mounted:function () {
        this.initTree();
        if(this.role.dataScope == '9'){
            this.initOfficeTree();
        }
    }
})